﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class EditAgent : Form
    {
        private bool bEditing = false;
        private string sEditingAgent = "";
        private DateTime createdDate = DateTime.MinValue;

        public EditAgent()
        {
            InitializeComponent();
        }

        public EditAgent(string agentCode)
        {
            sEditingAgent = agentCode;
            InitializeComponent();
            AcceptButton = buttonOK;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            Agent agent = new Agent();
            agent.Code = sEditingAgent;
            agent.Name = textBoxAgentName.Text;
            agent.Address_1 = textBoxAddress1.Text;
            agent.Address_2 = textBoxAddress2.Text;
            agent.Telephone1 = textBoxTelephone.Text;
            agent.Fax = textBoxFax.Text;
            agent.Interest_Rate = double.Parse(textBoxInterestRate.Text);
            if (createdDate == DateTime.MinValue)
            {
                agent.CreatedDate = DateTime.Today;
            }
            else
            {
                agent.CreatedDate = createdDate;
            }

            try
            {
                AgentServices.Update(agent);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to update agent information\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Agent information updated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private bool validateInputs()
        {
            double fMarginLimit = 0;
            if (double.TryParse(textBoxInterestRate.Text, out fMarginLimit) == false)
            {
                MessageBox.Show("Invalid Interest Rate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void EditAgent_Load(object sender, EventArgs e)
        {
            if (sEditingAgent != "")
            {
                comboBoxAgentCode.Items.Add(sEditingAgent);
                comboBoxAgentCode.SelectedIndex = 0;
                comboBoxAgentCode.Enabled = false;
                bEditing = true;
                buttonGO_Click(null, null);
                return;
            }

            try
            {
	            AgentCollection agentCollection = AgentServices.GetAgentCollection();
	
	            for (int iI = 0; iI < agentCollection.Count; iI++)
	            {
	                comboBoxAgentCode.Items.Add(agentCollection[iI].Code);
	            }
            }
            catch (System.Exception ex)
            {
            	
            }

            //comboBoxAgentCode.SelectedIndex = 0;
            EnableControls(false);
        }

        private void buttonGO_Click(object sender, EventArgs e)
        {
            Agent agent = null;
            try
            {
                agent = AgentServices.GetAgentByCode(comboBoxAgentCode.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid Agent Code   " + comboBoxAgentCode.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (agent == null)
            {
                MessageBox.Show("Invalid Agent Code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            EnableControls(true);

            textBoxAgentName.Text = agent.Name;
            textBoxAddress1.Text = agent.Address_1;
            textBoxAddress2.Text = agent.Address_2;
            textBoxTelephone.Text = agent.Telephone1;
            textBoxFax.Text = agent.Fax;
            textBoxInterestRate.Text = agent.Interest_Rate.ToString();

            createdDate = agent.CreatedDate;

            bEditing = true;
            sEditingAgent = comboBoxAgentCode.Text;
        }

        private void EnableControls(bool bEnable)
        {
            textBoxAgentName.Enabled = bEnable;
            textBoxAddress1.Enabled = bEnable;
            textBoxAddress2.Enabled = bEnable;
            textBoxTelephone.Enabled = bEnable;
            textBoxFax.Enabled = bEnable;
            textBoxInterestRate.Enabled = bEnable;
            buttonOK.Enabled = bEnable;
        }

        private void comboBoxAgentCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (bEditing)
            //{
            //    bEditing = false;
            //    ClearFields();
            //    EnableControls(false);
            //}
        }

        private void ClearFields()
        {
            textBoxAgentName.Text = "";
            textBoxAddress1.Text = "";
            textBoxAddress2.Text = "";
            textBoxTelephone.Text = "";
            textBoxFax.Text = "";
        }

        private void comboBoxAgentCode_SelectionChangeCommitted(object sender, EventArgs e)
        {
            Agent agent = null;
            try
            {
                agent = AgentServices.GetAgentByCode(comboBoxAgentCode.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid agent code   " + comboBoxAgentCode.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                EnableControls(false);
                bEditing = false;
                return;
            }

            if (agent == null)
            {
                MessageBox.Show("Invalid agent code   " + comboBoxAgentCode.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                EnableControls(false);
                bEditing = false;
                return;
            }


            EnableControls(true);

            textBoxAgentName.Text = agent.Name;
            textBoxAddress1.Text = agent.Address_1;
            textBoxAddress2.Text = agent.Address_2;
            textBoxTelephone.Text = agent.Telephone1;
            textBoxFax.Text = agent.Fax;

            bEditing = true;
            sEditingAgent = comboBoxAgentCode.Text;
        }
    }
}
